package main

import (
	"encoding/xml"
	"zstackgo/spring"
)

type BeanConf struct {
	XMLName xml.Name `xml:"beans"`
	Beans   []Bean   `xml:"bean"`
}

// [Golang 解析xml文件标签带冒号( : )解决方案](https://blog.csdn.net/qq_24210767/article/details/102829205)
type Bean struct {
	XMLName xml.Name `xml:"bean"`
	Id      string   `xml:"id,attr"`
	Class   string   `xml:"class,attr"`
	Plugins []Plugin `xml:"http://zstack.org/schema/zstack plugin"`
}

type Plugin struct {
	Exts []Extension `xml:"http://zstack.org/schema/zstack extension"`
}

type Extension struct {
	Interface   string `xml:"interface,attr"`
	InstanceRef string `xml:"instance-ref,attr"`
	Order       string `xml:"order,attr"`
}

var (
	SourceTmp = `package {{.PkgName}}

import (
	"zstackgo/spring"
)

func init() {
	o := new({{.StructName}})
	{{- if .Exts}}
	spring.Register(o, "{{.StructId}}", []*spring.PluginExtension{
		{{- range .Exts}}
		{
			{{- if .Interface}}
			Interface: "{{.Interface}}",
			{{- end}}
			{{- if .InstanceRef}}
			InstanceRef: "{{.InstanceRef}}",
			{{- end}}
			{{- if .Order}}
			Order:     {{.Order}},
			{{- end}}
		},
		{{- end}}
	})
	{{- else}}
	spring.Register(o, "{{.StructId}}", nil)
	{{- end}}
}

type {{.StructName}} struct {
}
`

	InterfaceTmp = `package {{.PkgName}}

import (
	"reflect"

	"zstackgo/spring"
)

func init() {
	spring.RegisterInterface("{{.InterfaceId}}", reflect.TypeOf((*{{.InterfaceName}})(nil)).Elem())
}

type {{.InterfaceName}} interface {
}
`
)

type GoSource struct {
	PkgName    string
	StructName string
	StructId   string
	Exts       []spring.PluginExtension
}

type GoSourceInterface struct {
	PkgName       string
	InterfaceName string
	InterfaceId   string
}
